<?php
/**
 * (c) 2010 Philip Clarke, Bouncing Limited.
**/

if( SECURITY_READONLY_LOCK=='false' && SECURITY_READONLY_COUNTER==0){

  $fc->unlockRecursive(DIR_FS_CATALOG);
  $fc->unlock(dirname(DIR_FS_CATALOG));
  $fc->relock(DIR_FS_CATALOG.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->relock(DIR_FS_ADMIN.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value='".time()."' WHERE configuration_key='SECURITY_READONLY_COUNTER'";

  $query = $db->execute($sql);

}else

if( ( SECURITY_READONLY_LOCK=='false' && (time() - SECURITY_READONLY_COUNTER) > 2*60*60 ) || (SECURITY_READONLY_LOCK == 'true' && SECURITY_READONLY_COUNTER !=0 ) )
{

  $fc->relockRecursive(DIR_FS_CATALOG);
  $fc->relock(dirname(DIR_FS_CATALOG));
  $fc->relock(DIR_FS_CATALOG.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->relock(DIR_FS_ADMIN.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->unlockRecursive(preg_replace('/\/$/', '', DIR_FS_SQL_CACHE));

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value='true' WHERE configuration_key='SECURITY_READONLY_LOCK'";

  $query = $db->execute($sql);

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value='0' WHERE configuration_key='SECURITY_READONLY_COUNTER'";

  $query = $db->execute($sql);


}


$sql = "SELECT UNIX_TIMESTAMP(last_modified) AS lrun FROM ".TABLE_CONFIGURATION." WHERE configuration_key='SECURITY_READONLY_LOCK'";

$query = $db->execute($sql);

if( (time()-$query->fields['lrun']) > 12*60*60 ){

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET last_modified=NOW(), configuration_value='true' WHERE configuration_key='SECURITY_READONLY_LOCK'";

  $query = $db->execute($sql);

  $fc->relockRecursive(DIR_FS_CATALOG);
  $fc->relock(dirname(DIR_FS_CATALOG));
  $fc->relock(DIR_FS_CATALOG.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->relock(DIR_FS_ADMIN.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->unlockRecursive(preg_replace('/\/$/', '', DIR_FS_SQL_CACHE));


}

define('WARNING_ADMIN_DIR_EXISTS', '<strong>Please</strong> either rename the admin folder or while uploading a module an <em>admin</em> folder has been created so the contents need to be moved into <em>%s</em>. <p>Shop Upgraded does not require any editing of configuration files to move this folder, <ul><li>Log out of Admin (if you are logged in).</li><li>FTP into your server</li><li>rename the folder.</li></ul>You can then visit it under the new name. at %s/WHATEVER_YOU_CALL_IT/</p>');

if(file_exists(DIR_FS_CATALOG.'admin')){
  $messageStack->add(sprintf(WARNING_ADMIN_DIR_EXISTS, DIR_WS_ADMIN, preg_replace('/([^:])\/+/', '\1/', HTTP_SERVER.DIR_WS_CATALOG) ) , 'caution');
}


// check if the installer directory exists, and warn of its existence
if(file_exists(DIR_FS_CATALOG.'zc_install')){
  $messageStack->add(sprintf(WARNING_INSTALL_DIRECTORY_EXISTS, DIR_FS_CATALOG.'zc_install'), 'warning');
}

if(is_writeable(DIR_FS_CATALOG.'includes/configure.php')){
  $messageStack->add(sprintf(WARNING_CONFIG_FILE_WRITEABLE, DIR_FS_CATALOG.'includes/configure.php'), 'warning');
}

if(is_writeable(DIR_FS_ADMIN.'includes/configure.php')){
  $messageStack->add(sprintf(WARNING_CONFIG_FILE_WRITEABLE, DIR_FS_ADMIN.'includes/configure.php'), 'warning');
}



